'use strict';
module.exports = {
	/**
	 * XXXnameXXX
	 * @url client/pay/kh/ttmakeRecharge 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			db,
			_
		} = util;
		let {
			uid,
			out_trade_no,
			out_refund_no,
			item,
			tv_id,
			tv_name,
			appid,
			appname,


		} = data;
		let res = {
			code: 0,
			msg: ""
		};
		// 业务逻辑开始-----------------------------------------------------------
		console.log("item6666", item);
		if (userInfo.clue_token && !userInfo.clickid) {
			//clickid得有值，否则会在回传处被return
			userInfo.clickid = userInfo.clue_token;
		}
		// 查询用户用户支付次数
		let recharge_all = await vk.baseDao.count({
			dbName: "a_recharge", // 表名
			whereJson: { // 条件
				pay_status: 1,
				user_id: userInfo._id,
			}
		});
		console.log("支付次数==========", recharge_all);
			recharge_all = recharge_all+1
		//把订单信息提交到数据库保存
		res.add_order = await db.collection('a_recharge').add({
			recharge_all:recharge_all,
			tv_name: tv_name, // 充值来源
			out_trade_no: out_trade_no,
			out_refund_no: out_refund_no,
			user_id: userInfo._id,
			nickname: userInfo.nickname || '未绑定昵称',
			recharge_amount: item.recharge_amount,
			shifu_amount: 0,
			platform: "tt",
			transaction_id: "",
			pay_status: 0,
			bean: item.bean,
			subject: item.subject,
			remark: item.recharge_text,
			middleman_id: userInfo.middleman_id || 0,
			invite_code: userInfo.invite_code || 0,
			user_register_date: userInfo.register_date || 0,
			appid: appid || "NoAppid",
			appname: appname || "NoAppname",
			ad_id: userInfo.ad_id || 0, //巨量的广告主ID
			promotion_id: userInfo.promotion_id || 0,
			advertiser_id: userInfo.advertiser_id || 0,
			clickid: userInfo.clickid || "", //巨量的广告点击识别ID
			os_name: originalParam.context.osName || "", //用户充值时的系统
			_add_time: new Date().getTime(),
			ip: originalParam.context.CLIENTIP,
			tfid: userInfo.tfid || 0,
			tg_pt: "douyin", // 推广平台
			province: userInfo.province, //省份
			city: userInfo.city, //市
			pageId: userInfo.pageId, //oppo广告专属-网页ID
			commission_is: false, //记录订单是否已结算
			_add_time: new Date().getTime(),

		})


		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}